package com.infore.qualityControl.importData;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.infore.qualityControl.entity.EquipmentParameter;
import com.infore.qualityControl.repository.IEquipmentParameterRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jxl.Sheet;
import jxl.Workbook;

@Api(value = "导入数据接口", tags = {"导入数据接口" })
@RestController
@RequestMapping("/import")
public class ImportDataController {
	
	@Autowired
	IEquipmentParameterRepository IEquipmentParameterRepository;
	
	@ApiOperation(value = "导入设备参数数据", notes = "")
	@RequestMapping(value = "/equipmentData", method = RequestMethod.GET)
	public String importEquipmentParameterData() {
		List<EquipmentParameter> list = new ArrayList<EquipmentParameter>();
		try {
			File filename = new File("/Users/e/Downloads/国家水站设备参数数据.xls");
	        Workbook rwb = Workbook.getWorkbook(filename);
	        Sheet rs = rwb.getSheet("Sheet1");//或者rwb.getSheet(0)
//	        int clos = rs.getColumns();//得到所有的列
	        int rows = rs.getRows();//得到所有的行
	        for (int i = 1; i < rows; i++) {
	        	EquipmentParameter equipmentParameter = new EquipmentParameter();
                String stationCode = rs.getCell(0, i).getContents(); //站点编码
                equipmentParameter.setStationCode(stationCode);
                String stationName = rs.getCell(1, i).getContents(); //站点名称
                equipmentParameter.setStationName(stationName);
                String indexCode = rs.getCell(2, i).getContents(); //指标编码
                equipmentParameter.setIndexCode(indexCode);
                String indexName = rs.getCell(3, i).getContents(); //指标名称
                equipmentParameter.setIndexName(indexName);
                String parameterCode = rs.getCell(4, i).getContents(); //设备参数编码
                equipmentParameter.setParameterCode(parameterCode);
                String parameterName = rs.getCell(5, i).getContents(); //设备参数名称
                equipmentParameter.setParameterName(parameterName);
                String parameterValue = rs.getCell(6, i).getContents(); //设备参数值
                equipmentParameter.setParameterValue(parameterValue);
                list.add(equipmentParameter);
	        }
		} catch (Exception e) {
	        e.printStackTrace();
	    }
		IEquipmentParameterRepository.saveAll(list);
		return "成功插入设备参数数据" + list.size() + "条";
	}
}
